package com.pa.scratch.controller;


import com.github.pagehelper.PageInfo;
import com.lww.springcloudapi.entity.TabGgcardObtain;
import com.lww.springcloudapi.result.Result;
import com.lww.springcloudapi.result.ResultCodeEnum;
import com.netflix.hystrix.contrib.javanica.annotation.HystrixCommand;
import com.pa.scratch.service.TabGgcardObtainService;
import io.swagger.annotations.Api;
import lombok.val;
import org.apache.ibatis.annotations.Param;
import org.apache.poi.hssf.usermodel.*;
import org.apache.velocity.runtime.directive.Foreach;
import org.checkerframework.checker.units.qual.A;
import org.eclipse.jetty.util.StringUtil;
import org.eclipse.jetty.util.ajax.JSON;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.List;

/**
 * <p>
 * 刮刮卡领取日志表 前端控制器
 * </p>
 *
 * @author SHEN
 * @since 2021-03-11
 */
@Api(description = "刮奖卡领取日/月报表")
@RestController
    @RequestMapping("/userTest/tab-ggcard-obtain")
public class TabGgcardObtainController {
    @Autowired
    TabGgcardObtainService tabGgcardObtainService;

    @PostMapping("/scratch/selectList")
    @HystrixCommand(fallbackMethod = "hystrixsSelectId2")
    public Result selectList(TabGgcardObtain tabGgcardObtain, @RequestParam(defaultValue = "1") int pageNo,
                                                              @RequestParam(defaultValue = "2") int pageSize) {
        try {
            List<TabGgcardObtain> tabGgcardObtains = tabGgcardObtainService.selectScratch(tabGgcardObtain, pageNo, pageSize);
//            for (TabGgcardObtain tabGgcardObtains1 :
//                    tabGgcardObtains) {
//                System.out.println(tabGgcardObtains1.toString());
//            }
            PageInfo<TabGgcardObtain> pageInfo = new PageInfo<TabGgcardObtain>(tabGgcardObtains);
//        return JSON.toString(pageInfo);
            return new Result().ok(pageInfo);
        } catch (Exception e) {
            return new Result().fail(ResultCodeEnum.FAIL);
        }
    }

    //备选方法
    public TabGgcardObtain hystrixsSelectId2(@PathVariable("id") Integer id) {
        return new TabGgcardObtain().setRemarks("id=>" + id + "没有对应的信息，null--Hystrix no this database in MySQL");
    }

    @RequestMapping(value = "/scratch/GetTheScratchExcelDownloads", method = RequestMethod.GET)
    public Result downloadAllClassmate(HttpServletResponse response, TabGgcardObtain tabGgcardObtain,
                                       @RequestParam(defaultValue = "1") int pageNo,
                                       @RequestParam(defaultValue = "2") int pageSize) throws IOException {
        HSSFWorkbook workbook = new HSSFWorkbook();
        HSSFSheet sheet = workbook.createSheet("信息表");

//        tabGgcardObtain.setAcptsiteid("衡阳");
//        tabGgcardObtain.setSvcid("2");
//        System.out.println(tabGgcardObtain.getAcptsiteid());
//        System.out.println(tabGgcardObtain.getObtainstate());
//        tabGgcardObtain.setY("2020");
//        tabGgcardObtain.setM("06");
//        tabGgcardObtain.setD("01");
        List<TabGgcardObtain> tabGgcardObtains = tabGgcardObtainService.selectScratch(tabGgcardObtain, pageNo, pageSize);

//        for (TabGgcardObtain tabGgcardObtains1 :
//                tabGgcardObtains) {
//            System.out.println(tabGgcardObtains1.toString());
//        }
        String fileName = "刮奖卡领取日/月报表" + ".xls";//设置要导出的文件的名字
        //新增数据行，并且设置单元格数据

        int rowNum = 1;

        String[] headers = {"刮奖卡序号", "营业厅", "状态", "业务类型", "领取日期", "用户手机号"};
        //headers表示excel表中第一行的表头
        HSSFRow row = sheet.createRow(0);
        //在excel表中添加表头

        for (int i = 0; i < headers.length; i++) {
            HSSFCell cell = row.createCell(i);
            HSSFRichTextString text = new HSSFRichTextString(headers[i]);
            cell.setCellValue(text);
        }
        //在表中存放查询到的数据放入对应的列
        for (TabGgcardObtain tabGgcardObtains1 : tabGgcardObtains) {
            HSSFRow row1 = sheet.createRow(rowNum);
            SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
            String ObtainDate = format.format(tabGgcardObtains1.getOptrdate());//这个就是把时间戳经过处理得到期望格式的时间
            row1.createCell(0).setCellValue(tabGgcardObtains1.getTabGgcardEncash().getCardno());
            row1.createCell(1).setCellValue(tabGgcardObtains1.getTabCode().getCodeName());
            row1.createCell(2).setCellValue(tabGgcardObtains1.getObtainstate());
            row1.createCell(3).setCellValue(tabGgcardObtains1.getSvcid());
            row1.createCell(4).setCellValue(ObtainDate);
            row1.createCell(5).setCellValue(tabGgcardObtains1.getTabHallUsermessage().getPnumber());
            rowNum++;
        }

        response.setContentType("application/octet-stream");
        response.setHeader("Content-disposition", "attachment;filename=" + fileName);
        response.flushBuffer();
        workbook.write(response.getOutputStream());

        return new Result().ok();
    }

}

